<?php
    include('checkAdminAccess.php');
    $iniVars = parse_ini_file('../../config.ini',TRUE);
    include('../classes/adodb5/adodb.inc.php');
    include('../classes/class.upload.php');
    include('../function/db.func.php');
    
    $action = $_POST['action'];
    
    if($action=='orderImg')
    {
        $courseImgImgOrder = explode('|',$_POST['courseImgImgOrder']);
        
        $i = 1;
        
        $result = array();
        
        foreach($courseImgImgOrder as $value)
        {
            $sql = 'UPDATE course_img SET ci_position='.($i++).' WHERE ci_id='.$value;
            $result2 = $db->Execute($sql);
            if($result2)
            {
                $result[] = 'success';
            }
            else
            {
                $result[] = 'fail';
            }
        }
        
        if(in_array('fail',$result))
        {
            echo 'fail';
        }
        else
        {
            echo 'success';
        }
    }
    else if($action=='deleteImg')
    {
        $courseImgImgId = $_POST['courseImgImgId'];
        
        $sql = "SELECT ci_path FROM course_img WHERE ci_id=$courseImgImgId";
        $result = $db->Execute($sql);
        $img_path = $result->fields['ci_path'];
        
        $sql = "DELETE FROM course_img WHERE ci_id=$courseImgImgId";
        $result = $db->Execute($sql);
        
        if($result)
        {
            if(unlink($iniVars['Locations']['sitepath'].$img_path))
            {
                echo 'success';
            }
            else
            {
                echo 'Fail1';
            }
        }
        else
        {
            echo 'Fail2';
        }
        
        $result->Close();
    }
    else if($action=='uploadImg')
    {
        $files = array();
        foreach($_FILES['courseImgImgVar'] as $k => $l)
        {
            foreach ($l as $i => $v)
            {
                if (!array_key_exists($i, $files))
                    $files[$i] = array();
                $files[$i][$k] = $v;
            }
        }
    
        foreach ($files as $file)
        {
            if($file['name']!='')
            {
                $handle = new Upload($file);
                if ($handle->uploaded)
                {
                     $handle->image_resize = true;
                    $handle->image_x = 210;
                    $handle->image_y = 140;
                    $handle->image_ratio_fill = true;
                    $handle->Process($iniVars['Locations']['sitepath'].'images/course/en_4');
        
                    if ($handle->processed)
                    {
                        $sql = 'INSERT INTO course_img VALUES("","13","images/course/en_4/'.$handle->file_dst_name.'","4")';
                        $result =$db->Execute($sql);
                        if($result)
                        {
                            $msg[] = 'success';
                        }
                        else
                        {
                            unlink($iniVars['Locations']['sitepath'].'images/course/en_4/'.$handle->file_dst_name);
                            $msg[] = 'fail';
                        }
                    }
                    else
                    {
                        $msg[] = 'fail';
                    }
        
                }
                else
                {
                    $msg[] = 'fail';
                }
            }
        }
        
        if(in_array('fail',$msg))
        {
             echo <<<js
            <script src="{$iniVars['Locations']['siteurl']}js/jquery.js" type="text/javascript"></script>
            <script type="text/javascript">
                $(".img_submit_indicator", top.document).css('display','none');
                $("#img_error_msg", top.document).html('ไม่สามารถอัพโหลดรูปภาพได้ กรุณาลองใหม่อีกครั้ง');
                $("#img_error_msg", top.document).css('display','block');
                setInterval("top.location='../index.php?page=course&part=course19';",3000);
            </script>
js;
        }
        else
        {
             echo <<<js
            <script src="{$iniVars['Locations']['siteurl']}js/jquery.js" type="text/javascript"></script>
            <script type="text/javascript">
                $(".img_submit_indicator", top.document).css('display','none');
                $("#img_success_msg", top.document).html('อัพโหลดรูปภาพเรียบร้อยแล้ว กรุณารอสักครู่');
                $("#img_success_msg", top.document).css('display','block');
                setInterval("top.location='../index.php?page=course&part=course19';",3000);
            </script>
js;
        }
    }
    else if($action=='msg')
    {
        $sql = 'UPDATE course_data SET cd_value="'.$_POST['editor1'].'" WHERE cd_id=9';
        $result = $db->Execute($sql);
        if($result)
        {
            $error = FALSE;
            $msg = "<br />แก้ไขข้อมูลเรียบร้อยแล้ว";
        }
        else
        {
            $error = TRUE;
            $msg = "<br />ไม่สามารถแก้ไขข้อมูลได้ กรุณาลองใหม่อีกครั้ง.";
        }
        
        
         if($error)
        {
            echo <<<js
            <script src="{$iniVars['Locations']['siteurl']}js/jquery.js" type="text/javascript"></script>
            <script type="text/javascript">
                $(".msg_submit_indicator", top.document).css('display','none');
                $("#msg_error_msg", top.document).html('{$msg}');
                $("#msg_error_msg", top.document).css('display','block');
                setInterval("top.location='../index.php?page=course&part=course19';",3000);
            </script>
js;
        }
        else
        {
            echo <<<js
            <script src="{$iniVars['Locations']['siteurl']}js/jquery.js" type="text/javascript"></script>
            <script type="text/javascript">
                $(".msg_submit_indicator", top.document).css('display','none');
                $("#msg_success_msg", top.document).html('{$msg}');
                $("#msg_success_msg", top.document).css('display','block');
                setInterval("top.location='../index.php?page=course&part=course19';",3000);
            </script>
js;
        }
    }
?>